﻿using Furion.DependencyInjection;
using LS.Nc6.Application.System;
using LS.Nc6.Core;
using LS.Nc6.Web.Entry.ViewModel.UI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections;


namespace LS.Nc6.Web.Entry.Controllers
{
    public class ChanUIController : Controller
    {
        private readonly ICmsChannelService _cmsChannelService;
        private readonly ISysFileService _fileService;
        private readonly ICmsContentService _contentService;
        private  readonly ICache _cache;

        
        public ChanUIController(ICmsChannelService cmsChannelService, ISysFileService fileService,
              ICmsContentService contentService,
            IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed) 
        {
            _cmsChannelService = cmsChannelService;
            _fileService = fileService;
            _contentService = contentService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }

        /// <summary>
        /// 频道首页
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Index(long id, int pageIndex=1)
        {
            var channelmodel= await  _cmsChannelService.GetByIdAsync(id);
            if (channelmodel == null) return RedirectToAction("NotFound", "Home");
            ViewBag.Title = channelmodel.ChannelName;
            CmsChannelVm cmsChannelVm = new CmsChannelVm();
            cmsChannelVm.ChanIndex = channelmodel.IndexName;
            cmsChannelVm.PageIndex = pageIndex;
            if (channelmodel.ParentId != 1)
            {
                channelmodel = await _cmsChannelService.GetByIdAsync(channelmodel.ParentId.Value);
                cmsChannelVm.ChanIndex = channelmodel.IndexName;
                cmsChannelVm.Id = id;
            }
            cmsChannelVm.ChanName = channelmodel.ChannelName;
            cmsChannelVm.ChanBackground =!string.IsNullOrEmpty(channelmodel.ImageUrlIds)? (await _fileService.GetByIdAsync(CommonUtil.GetLongList(channelmodel.ImageUrlIds)[0]))?.Fpath: "ui/image/channlbg.jpg";
            var cmschanlist = await _cmsChannelService.List(a => a.ParentId == channelmodel.Id, a => a.Taxis, a => new {  a.Id,  a.ChannelName, a.IndexName });
            List<CmsChannelChildVm> childlist = new List<CmsChannelChildVm>();
            foreach (var item in cmschanlist)
            {
                childlist.Add(new CmsChannelChildVm {  Id=item.Id, ChanIndex =item.IndexName,ChanName=item.ChannelName });
            }
            if (cmsChannelVm.Id==0)
            {
                cmsChannelVm.Id = childlist.Count > 0 ? childlist[0].Id : id;
            }
            cmsChannelVm.ChanChild = childlist;

            return View(cmsChannelVm);
        }
       


        #region 公有方法

        #endregion

        #region 私有方法

        #endregion

    }
}